<template>
    <div class="params">
    <my-bread :breadList="breadList"></my-bread>
     <el-card>
     <el-alert
    title="注意：只允许为第三级分类设置参数"
    type="warning"
    show-icon>
  </el-alert>
  <el-form label-width="120px">
  <el-form-item label="选择商品分类:">
   <el-cascader
   v-model="cate"
    :options="splist2"
    :props="addprops"
    clearable
    @change="cateChange"></el-cascader>
  </el-form-item>
  </el-form>
   <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="动态参数" name="many">
          <el-button type="primary" :disabled="disabled" @click="dialogVisible=true">添加参数</el-button>
          <el-table :data="manyList">
            <el-table-column type="expand">
              <template slot-scope="scope">
                <el-tag
                  :key="tag"
                  v-for="(tag,index) in scope.row.attr_vals"
                  closable
                  :disable-transitions="false"
                  @close="handleClose(scope.row,index)"
                >
                  {{ tag }}
                </el-tag>
                <el-input
                  class="input-new-tag"
                  v-if="scope.row.inputVisible"
                  v-model="scope.row.inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="handleInputConfirm(scope.row)"
                  @blur="handleInputConfirm(scope.row)"
                >
                </el-input>
                <el-button
                  v-else
                  class="button-new-tag"
                  size="small"
                  @click="showInput(scope.row)"
                  >+ New Tag</el-button
                >
              </template>
            </el-table-column>
            <el-table-column type="index" label="#"></el-table-column>
            <el-table-column
              label="参数名称"
              prop="attr_name"
            ></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(scope.row)"
                  >编辑</el-button
                >
                <el-button type="danger" icon="el-icon-delete" size="mini" @click="del(scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="静态属性" name="only">
          <el-button type="primary" :disabled="disabled" @click="dialogVisible=true">添加属性</el-button>
          <el-table :data="onlyList">
            <el-table-column type="expand">
              <template slot-scope="scope">
                <el-tag
                  :key="tag"
                  v-for="(tag, index) in scope.row.attr_vals"
                  closable
                  :disable-transitions="false"
                  @close="handleClose(scope.row, index)"
                >
                  {{ tag }}
                </el-tag>
                <el-input
                  class="input-new-tag"
                  v-if="scope.row.inputVisible"
                  v-model="scope.row.inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="handleInputConfirm(scope.row)"
                  @blur="handleInputConfirm(scope.row)"
                >
                </el-input>
                <el-button
                  v-else
                  class="button-new-tag"
                  size="small"
                  @click="showInput(scope.row)"
                  >+ New Tag</el-button
                >
              </template>
            </el-table-column>
            <el-table-column type="index" label="#"></el-table-column>
            <el-table-column
              label="属性名称"
              prop="attr_name"
            ></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(scope.row)"
                  >编辑</el-button
                >
                <el-button type="danger" icon="el-icon-delete" size="mini" @click="del(scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
     </el-card>
     <el-dialog
  :title="activeName=='many'?'添加动态参数':'添加静态属性'"
  :visible.sync="dialogVisible"
  width="30%">
  <el-form  :model="form" label-width="80px">
  <el-form-item :label="activeName=='many'?'动态参数':'静态属性'">
    <el-input v-model="form.attr_name"></el-input>
  </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addOk">确 定</el-button>
  </span>
</el-dialog>
 <el-dialog
  :title="activeName=='many'?'修改动态参数':'修改静态属性'"
  :visible.sync="dialogEditVisible"
  width="30%">
  <el-form  :model="editform" label-width="80px">
  <el-form-item :label="activeName=='many'?'动态参数':'静态属性'">
    <el-input v-model="editform.attr_name"></el-input>
  </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogEditVisible = false">取 消</el-button>
    <el-button type="primary" @click="editOk">确 定</el-button>
  </span>
</el-dialog>
    </div>
</template>

<script>
import {
  hqgoods,
  categoriesAttributes,
  addcs,
  editcs,
  delcs
} from "@/http/goodsadd.js";
export default {
  data() {
    return {
      attr_id:"",
      editform: {
        attr_name: ""
      },
      dialogEditVisible:false,
      form: {
        attr_name: ""
      },
      dialogVisible: false,
      onlyList: [],
      manyList: [],
      cate: [],
      addprops: {
        label: "cat_name",
        value: "cat_id",
        checkStrictly: true
      },
      splist2: [],
      activeName: "many",
      inputValue: "",
      inputVisible: false,
      options: [],
      breadList: [
        {
          path: "/home",
          title: "首页"
        },
        {
          path: "",
          title: "商品管理"
        },
        {
          path: "/params",
          title: "参数列表"
        }
      ]
    };
  },
  created() {
    this.addCate();
  },
  computed: {
    disabled() {
      return this.cate.length == 3 ? false : true;
    }
  },
  methods: {
    async editOk(){
     let res=await editcs(this.cate[this.cate.length - 1],this.attr_id,{attr_name:this.editform.attr_name,attr_sel:this.activeName})
     this.dialogEditVisible=false
     this.categoriesAttributes(this.activeName)
      
    },
    // 编辑
    edit(row) {
     this.dialogEditVisible=true
     this.editform.attr_name=row.attr_name
      this.attr_id=row.attr_id
    },
    // 删除
    async  del(row) {
      let res=await delcs(this.cate[this.cate.length - 1],row.attr_id,{attr_sel:this.activeName})
      this.categoriesAttributes(this.activeName)
    },
    // 添加动态参数
    async addOk() {
      let res = await addcs(this.cate[this.cate.length - 1],{attr_name:this.form.attr_name,attr_sel:this.activeName});
      this.dialogVisible=false
      this.categoriesAttributes(this.activeName)
    },
    // 获取动态参数和静态属性
    async categoriesAttributes(sel) {
      let res = await categoriesAttributes(this.cate[this.cate.length - 1], {
        sel
      });
      // console.log(res.data);
      res.data.forEach((v, i) => {
        v.attr_vals = v.attr_vals ? v.attr_vals.split(",") : [];
        v.inputVisible = false;
        v.inputValue = "";
        
      });
      // console.log("返回值", res);
      // console.log(sel);
      if ((sel == "many")) {
        this.manyList = res.data;
      } else {
        this.onlyList = res.data;
        console.log(this.onlyList);
      }
    },
    // tab切换
    handleClick(tab) {
      if (this.cate.length == 3) {
        this.categoriesAttributes(tab.name);
        console.log(tab.name);
      }
    },
    // 改变分类
    cateChange() {
      if (this.cate.length == 3) {
        this.categoriesAttributes(this.activeName);
        console.log(this.activeName);
      }
    },
    // 获取分类
    async addCate() {
      let res = await hqgoods();
      console.log(res.data);
      this.splist2 = res.data;
    },
    // 删除tag
    async handleClose(row, index) {
      console.log(row);
      row.attr_vals.splice(index, 1);
      let attr_vals = row.attr_vals.join(",");
      // console.log("参数的属性值",attr_vals);
      let res = await editcs(this.cate[this.cate.length - 1], row.attr_id, {
        attr_name: row.attr_name,
        attr_sel: row.attr_sel,
        attr_vals: attr_vals
      });
    },
    // input 显示
    showInput(row) {
      row.inputVisible = true;
      this.$nextTick((_)=> {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    // 回车是件
    async handleInputConfirm(row) {
      // console.log(row.inputValue);
      if (row.inputValue) {
        row.attr_vals.push(row.inputValue);
        let attr_vals = row.attr_vals.join(",");
        let res = await editcs(this.cate[this.cate.length - 1], row.attr_id, {
          attr_name: row.attr_name,
          attr_sel: row.attr_sel,
          attr_vals: attr_vals
        });
      }
        row.inputVisible = false;
        row.inputValue = "";
    },

  }
};
</script>

<style lang="scss">
.el-cascader-panel {
  height: 300px;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
